***************
* This file analyses the data of ads directly reported to Job-Room
* Author: Daniel Kopp
***************

clear
clear matrix
clear mata

set maxvar 8000
set seed 1

use "data_processed\oste.dta"

* Match occupations to the dataset
preserve
	use  "data_processed\oste_occupations.dta", clear

	drop abschluss_inland abschluss_ausland		// abschluss_ausland has only missings and 93% of abschluss_inland are missing as well
	
	* We drop ergaenzende_angaben
	drop ergaenzende_angaben
	
	* Sometimes, the only variation within oste_id_avam comes from different experience or education levels
	* We take within oste_id_avam the maximum experience and education level if there is variation
	foreach var of varlist  cod_erfahrung cod_ausbildung  {
		bys oste_id_avam : egen max_`var' = max(`var') if `var'!=.
		drop `var' 
		rename max_`var' `var'
	}
	
	duplicates drop oste_id_avam cod_b_avam cod_isco5  cod_erfahrung cod_ausbildung 	, force
	
	duplicates drop oste_id_avam cod_b_avam  	, force		// 3 obs dropped because there has been an error in the original dataset (wrong assignment of isco to avam code)
	
	drop cod_b_avam
			
	tostring  cod_isco5, gen(cod_isco5_str)

	gen 	isco4_str 	= substr(cod_isco5_str,1,strlen(cod_isco5_str)-1)
	replace isco4_str = "-1" if isco4_str=="-"
	destring isco4_str	, gen(isco_4)
	drop isco4_str cod_isco5_str cod_isco5
	
	duplicates drop oste_id_avam isco_4  	, force	
	
	bys oste_id_avam (n_occup): gen n = _n
	drop n_occup
	
	rename isco_4 isco_4_
	
	reshape wide isco_4_ , i(oste_id_avam) j(n)
	tempfile oste_occup_wide
	save `oste_occup_wide'
restore

merge m:1 oste_id_avam using `oste_occup_wide'
drop if _merge==2		
drop _merge

replace isco_4_1=isco_4_2 if isco_4_1==-1
replace isco_4_2=isco_4_3 if isco_4_2==-1

* Gen dummy if many occupations have been assigned to a job ad
gen 	more_1occup_per_ad = 0
replace	more_1occup_per_ad = 1 if isco_4_2!=.

gen 	more_2occup_per_ad = 0
replace	more_2occup_per_ad = 1 if isco_4_3!=.

tab more_1occup_per_ad,m		// 82.3% of the ads exhibit no variation in the isco-4 code  
tab more_2occup_per_ad,m		// 95.8% of the ads have max two isco-2 codes

* We keep only the first two occupations
forvalues i = 3/27 {
    drop isco_4_`i'
}

* Gen Isco 3 digit
forvalues x = 1/2 {
	tostring  isco_4_`x', gen(isco_4_`x'_str)
	gen 	isco_3_`x'_str 	= substr(isco_4_`x'_str,1,strlen(isco_4_`x'_str)-1)
	destring isco_3_`x'_str	, gen(isco_3_`x')
	drop isco_4_`x'_str isco_3_`x'_str 
}

* Gen Isco 2 digit
forvalues x = 1/2 {
	tostring  isco_3_`x', gen(isco_3_`x'_str)
	gen 	isco_2_`x'_str 	= substr(isco_3_`x'_str,1,strlen(isco_3_`x'_str)-1)
	destring isco_2_`x'_str	, gen(isco_2_`x')
	drop isco_3_`x'_str isco_2_`x'_str 
}

* Gen Isco 1 digit
forvalues x = 1/2 {
	tostring  isco_2_`x', gen(isco_2_`x'_str)
	gen 	isco_1_`x'_str 	= substr(isco_2_`x'_str,1,strlen(isco_2_`x'_str)-1)
	destring isco_1_`x'_str	, gen(isco_1_`x')
	drop isco_2_`x'_str isco_1_`x'_str 
	replace isco_1_`x' = 0 if inrange(isco_2_`x',1,3)
}


forvalues i = 1/4 {
cap run "Help_files\label_isco08_`i'.do"
label values isco_`i'_1 isco08_`i'_lab
label values isco_`i'_2 isco08_`i'_lab
}

label values isco_2_1 isco08_2_lab
label values isco_2_2 isco08_2_lab

tab isco_2_1,m
tab isco_1_1,m

foreach i in 10 20 30 40 50 70 80 90 {
	recode isco_2_1 (`i'=.)
	recode isco_2_2 (`i'=.)
}

* In the following, only the first occupation is used:
gen isco_08_1 = isco_1_1
gen isco_08_2 = isco_2_1
gen isco_08_3 = isco_3_1
gen isco_08_4 = isco_4_1

forvalues i = 1/4 {
label values isco_08_`i' isco08_`i'_lab
}

* Gen canton of the firm
merge m:1 cod_arbeitsort using "Help_files\label_arbeitsort"
drop _merge

gen cod_gemeinde = cod_arbeitsort
merge m:1 cod_gemeinde using "Misc_files\key_gemeinde_kanton.dta"
drop if _merge==2
drop _merge

label define kanton_lab 1 "ZH" 2 "BE" 3 "LU" 4 "UR" 5 "SZ" 6 "OW" 7 "NW" 8 "GL" 9 "ZG" 10 "FR" 11 "SO" 12 "BS" 13 "BL" 14 "SH" 15 "AR" 16 "AI" 17 "SG" 18 "GR" 19 "AG" 20 "TG" 21 "TI" 22 "VD" 23 "VS" 24 "NE" 25 "GE" 26 "JU"
label values cod_kanton kanton_lab

drop cod_gemeinde txt_kanton txt_gemeinde


******************************
* Descriptive stats
******************************

* Our sample period covers 1.7.2018 until 30.6.2021
count		// 563,444 job ads

distinct burnr_ag  // 75,625 distinct establishments


******************************
* Merge other data to the dataset
******************************

* Merge female share in employment (from Swiss wage structure survey) to dataset:
gen isco_2 = isco_08_2

merge m:1 isco_2 using "Misc_files\lse_isco_women.dta"
drop if _merge==2
drop _merge

rename woman women_isco_lse

* Merge the number of employees per ISCO 4-digit occupation from the Structural Survey ("Strukturerhebung") to our dataset. 
* We then have the full set of isco 4-digit occupations (498, or 495 if we ignore the 3-digit codes - see below). 
gen s_isco_08_4 = isco_08_4
merge m:1 s_isco_08_4 using "Misc_files\employment_by_isco4.dta", keepusing(employed_isco4)
drop if _merge==2
drop _merge

* Gen variable for full-/part-time job 
gen 	pensum_cat = 0 if num_pensum_von==100		// only full-time
replace pensum_cat = 1 if num_pensum_von<100 & num_pensum_bis==100 // full-time or part-time
replace pensum_cat = 2 if num_pensum_bis<100 // only part-time

label define pensum 0 "Full-time job" 1 "Full- or part-time job" 2 "Part-time job"
label values pensum_cat pensum

* Gen dummies for detailed part-time categories
gen 	pensum_det_10 = num_pensum_bis==100
forvalues i = 0/9 {
		gen 	pensum_det_`i' = 0
		forvalues x = 0/9 {
			replace pensum_det_`i' = 1 if inrange(`i'`x',num_pensum_von,num_pensum_bis)		
		}
}

gen fulltime = pensum_cat==0
gen parttime = pensum_cat==2
gen part_or_fulltime = inrange(pensum_cat,1,2)


***
* Descriptive statistics on explicit gender preference (confidential information)
***

count if inrange(preferred_gender,0,1)
recode preferred_gender (.=999)
tab preferred_gender,m		// 5.2% (29,150 job ads) state a preference for men or women

gen men = preferred_gender==0
gen women = preferred_gender==1
gen gender_pref = inrange(preferred_gender,0,1)
gen gender_neutral = preferred_gender==999


****************
* Calculate share of ads with a female/male/neutral preference per isco 2
****************

tab men if preferred_gender!=999,m	// 52% of the  non-gender neutral job ads have a preference for men and 48% for women 

eststo clear
quietly estpost tabstat men, by(pensum_cat) notot
estimates store men
quietly estpost tabstat women, by(pensum_cat) notot
estimates store women
quietly estpost tabstat gender_neutral, by(pensum_cat) notot
estimates store gender_neutral

esttab men women gender_neutral, cells("mean(fmt(%9.2gc))") label noobs collabels(none) varlabels(`e(labels)')  ///
		mtitle("\makecell{Preference \\ for men}"  "\makecell{Preference \\ for women}" "\makecell{No recorded \\ preference}") ///
		title(Gender preference by number of working hours) 	

	
******************************
* Which firms state a gender preference?
******************************
cap drop _merge
foreach var in noga_6st unternehmensgroesse rechtsform {
    merge m:1 cod_`var' using "Help_files\label_`var'.dta"
	drop if _merge==2
	drop _merge
}

run "Help_files\labelfromstring.do"
foreach var in noga_6st unternehmensgroesse rechtsform {
	labelfromstring, stringvar(txt_`var') numvar(cod_`var') 
}

tostring  cod_noga_6st, gen(cod_noga_6st_str)

forvalues i = 1/5 {
gen noga_`i'_str 	= substr(cod_noga_6st_str,1,`i')
destring noga_`i'_str	, gen(noga_`i')
drop noga_`i'_str
}

recode burnr_ag (-1=.)
recode burnr_auftraggeber (-1=.)

gen 	long num_bur = burnr_ag 
replace num_bur = burnr_auftraggeber if burnr_auftraggeber!=.

gen  	unt_groesse = 999 if cod_unternehmensgroesse==. | cod_unternehmensgroesse==-1 | cod_unternehmensgroesse==14
replace unt_groesse = 1 if inrange(cod_unternehmensgroesse,15,18)  // 0 to 9
replace unt_groesse = 2 if inrange(cod_unternehmensgroesse,19,20)	// 10 to 49
replace unt_groesse = 3 if inrange(cod_unternehmensgroesse,21,23) 	// 50 to 249 
replace unt_groesse = 4 if cod_unternehmensgroesse>23				// more than 249
tab unt_groesse

* Gen NOGA Abschnitte 
preserve 
	import excel "Misc_files\branchen.xlsx", ///
		sheet("Tabelle1") firstrow case(lower) clear
	tempfile noga
	save `noga'
restore 
gen noga2 = noga_2
merge m:1 noga2 using `noga', nogen keep(1 3)
labelfromstring, stringvar(noga_str) numvar(noga_num)
rename noga_num noga
tab noga, m 
drop noga_str noga2 noga1

* Legal status
recode cod_rechtsform ///
	(28 30/31 = 28 "Personengesellschaft") ///
	(33 = 33 "Aktiengesellschaft") ///
	(34 = 34 "GmbH") ///
	(35 = 35 "Genossenschaft") ///
	(36/37 = 36 "Verein, Stiftung") ///
	(44/53 56/59 = 44 "Öffentlicher Sektor") ///
	(29 38/40 54 60 -1 = 999 "andere / unbekannt") ///
	, gen(rechtsform)
tab rechtsform

* We distinguish between those that never and sometimes state a gender preference
preserve
foreach var of varlist noga rechtsform unt_groesse {
	bys num_bur: egen m_`var'= mode(`var'), missing
}

collapse gender_pref m_noga m_rechtsform m_unt_groesse, by(num_bur)

gen 	gender_pref_cat = 0 if gender_pref==0					
replace gender_pref_cat = 1 if gender_pref>0 & gender_pref<.
tab gender_pref_cat,m				// In total, 88,220 different firms. 74,557 (84.5%) with no gender preferences, 13,663 firmst that sometimes state a gender preference

tab m_rechtsform,m

recode m_rechtsform (35=999)
recode m_rechtsform (36=999)
recode m_rechtsform (.=999)

tab m_noga,m 
recode m_noga (.=999)

tab m_noga, gen(noga_short_)
tab m_rechtsform, gen(rechtsform_)
tab m_unt_groesse, gen(unt_groesse_)


quietly estpost sum noga_short_* rechtsform_* unt_groesse_* if gender_pref_cat==0
eststo no_pref
quietly estpost sum noga_short_* rechtsform_* unt_groesse_* if gender_pref_cat==1
eststo sometimes_pref

*label var noga_short_0 "\quad Agriculture, forestry and fishing"
label var noga_short_1 "\quad Manufacturing"
label var noga_short_2 "\quad Construction"
label var noga_short_3 "\quad Wholesale and retail trade"
label var noga_short_4 "\quad Transportation and storage"
label var noga_short_5 "\quad Accomodation and food service activities"
label var noga_short_6 "\quad Information and communication"
label var noga_short_7 "\quad Financial and insurance services"
label var noga_short_8 "\quad Real estate activities"
label var noga_short_9 "\quad Professional, scientific and technical activities"
label var noga_short_10 "\quad Administrative support activities"
label var noga_short_11 "\quad Private employment agencies"
label var noga_short_12 "\quad Public administration"
label var noga_short_13 "\quad Education"
label var noga_short_14 "\quad Human health and social work activities"
label var noga_short_15 "\quad Arts, entertainment and recreation"
label var noga_short_16 "\quad Other service activities"
label var noga_short_17 "\quad No information"

label var unt_groesse_1 "\quad 1-9 employees"
label var unt_groesse_2 "\quad 10-49 employees"
label var unt_groesse_3 "\quad 50-249 employees"
label var unt_groesse_4 "\quad $>=$250 employees"
label var unt_groesse_5 "\quad No information"

label var rechtsform_1 "\quad Private company"
label var rechtsform_2 "\quad Stock company"
label var rechtsform_3 "\quad Limited liability company"
label var rechtsform_4 "\quad Public sector"
label var rechtsform_5 "\quad Other/no information"

esttab no_pref sometimes_pref , cells("mean(fmt(%9.2gc))") label  collabels(none) varlabels(`e(labels)')  ///
		mtitle("never" "sometimes" ) ///
		order("\textit{Industry}" noga_short_*  "\textit{Firm size}" unt_groesse_*  "\textit{Legal status}" rechtsform_*) ///
		title(Characteristics of firms with and without gender preference) 
esttab no_pref sometimes_pref  using "$results_part_time\table_d8.tex", replace ///
		cells("mean(fmt(%9.2gc))") label  collabels(none) varlabels(`e(labels)') frag ///
		mtitle("never" "sometimes" ) ///
		order("\textit{Industry}" noga_short_*  "\textit{Firm size}" unt_groesse_*  "\textit{Legal status}" rechtsform_*) ///		
		title(Characteristics of firms with and without gender preference) 
restore 	
	
******************************
* Relationship between full- and part-time jobs and a preference for men/women
******************************

* I focus on the 15'739 job vacancies (5.5%) for which recruiters state a preference for a gender 

replace num_bur = . if num_bur==-1
count if num_bur ==. & preferred_gender!=999  // 230  
	
********************************************
* Regress a dummy for men/women on fulltime/parttime: 
********************************************

foreach var in cod_erfahrung cod_ausbildung cod_unternehmensgroesse {
    recode `var' (-1=.)
}

* Transform the canton of a firm that posts a job ad into a categorical variable:
tab cod_kanton, gen(cod_kanton)

global controls "i.cod_kanton i.befristet i.sonnfeiertagsarbeit i.schichtarbeit i.nachtarbeit i.heimarbeit i.lehre i.cod_erfahrung i.cod_ausbildung i.cod_unternehmensgroesse"

foreach x in cod_kanton befristet sonnfeiertagsarbeit schichtarbeit nachtarbeit heimarbeit lehre cod_erfahrung cod_ausbildung cod_unternehmensgroesse {
    disp "`x'"
	recode `x' (. = 999) 
	tab `x'
}

eststo clear
local cond "  "
foreach x in   men women {
		quietly reg `x' i.pensum_cat `cond', cluster(num_bur)
			quietly sum  `x' 	if e(sample)==1
			local mean_disp	: disp %4.3f r(mean)
			estadd local mean_disp "`mean_disp'"
			estadd local controls "No"			
			estadd local noga "No"
			estadd local bn "No"
			estadd local firm "No"
		eststo no_cov_`x'_pens

		quietly reg `x' i.pensum_cat $controls `cond', cluster(num_bur)
			quietly sum  `x' 	if e(sample)==1
			local mean_disp	: disp %4.3f r(mean)
			estadd local mean_disp "`mean_disp'"
			estadd local controls "Yes"						
			estadd local noga "No"
			estadd local bn "No"
			estadd local firm "No"
		eststo contr_`x'_pens
		
		quietly reg `x'  i.pensum_cat $controls i.noga_2 `cond', cluster(num_bur)
			quietly sum  `x' 	if e(sample)==1
			local mean_disp	: disp %4.3f r(mean)
			estadd local mean_disp "`mean_disp'"	
			estadd local controls "Yes"									
			estadd local noga "Yes"
			estadd local bn "No"
			estadd local firm "No"
		eststo noga_`x'_pens

		quietly reg `x'  i.pensum_cat $controls i.noga_2 i.isco_08_4 `cond', cluster(num_bur)
			quietly sum  `x' 	if e(sample)==1
			local mean_disp	: disp %4.3f r(mean)
			estadd local mean_disp "`mean_disp'"	
			estadd local controls "Yes"									
			estadd local noga "Yes"
			estadd local bn "Yes"
			estadd local firm "No"
		eststo noga_bn_sep_`x'_pens

		quietly reghdfe `x'  i.pensum_cat $controls  `cond', absorb(isco_08_4 noga_2 num_bur)	cluster(num_bur)	// within firm and occupation
			quietly sum  `x' 	if e(sample)==1
			local mean_disp	: disp %4.3f r(mean)
			estadd local mean_disp "`mean_disp'"	
			estadd local controls "Yes"									
			estadd local noga "Yes"
			estadd local bn "Yes"
			estadd local firm "Yes"
		eststo firm_bn_`x'_pens	
}

* Dep. var: preference for men
	esttab no_cov_men_pens contr_men_pens noga_men_pens noga_bn_sep_men_pens  firm_bn_men_pens ,  ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none) nomtitles nodepvar star(* 0.1 ** 0.05 *** 0.01)    ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			title(Dep. var: preference for men) ///
			stats(controls noga bn firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation" "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")	
	esttab no_cov_men_pens contr_men_pens noga_men_pens noga_bn_sep_men_pens  firm_bn_men_pens using "$results_part_time\table_1.tex", replace   ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none) nomtitles nodepvar star(* 0.1 ** 0.05 *** 0.01)  frag  ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			title(Dep. var: preference for men) ///
			stats(controls noga bn  firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation"  "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")
			
* Dep. var: preference for women
	esttab no_cov_women_pens contr_women_pens noga_women_pens noga_bn_sep_women_pens  firm_bn_women_pens ,  ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none) nomtitles nodepvar star(* 0.1 ** 0.05 *** 0.01)    ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			title(Dep. var: preference for women) ///
			stats(controls noga bn firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation" "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")	
	esttab no_cov_women_pens contr_women_pens noga_women_pens noga_bn_sep_women_pens  firm_bn_women_pens using "$results_part_time\table_d9.tex", replace   ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none) nomtitles nodepvar star(* 0.1 ** 0.05 *** 0.01)  frag  ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			title(Dep. var: preference for women) ///
			stats(controls noga bn  firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation"  "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")	


			
*****
* Robustness
*****

* Only temporary jobs 

tab cod_frist_typ, gen(cod_frist_typ_num)
tab cod_frist_typ_num2,m

foreach y in  temporary {
if "`y'"=="temporary" {
		local cond "if  cod_frist_typ_num2==1 | cod_frist_typ_num3==1 "
}	
	foreach x in   men women {
			quietly reghdfe `x'  i.pensum_cat $controls  `cond', absorb(isco_08_4 noga_2 num_bur)	cluster(num_bur)	// within firm and occupation
				quietly sum  `x' 	if e(sample)==1
				local mean_disp	: disp %4.3f r(mean)
				estadd local mean_disp "`mean_disp'"	
				estadd local controls "Yes"	
				estadd local noga "Yes"
				estadd local bn "Yes"
				estadd local noga_bn	"No"	
				estadd local firm "Yes"
			eststo firm_bn_`x'_`y'
	}
}

	esttab  firm_bn_men_pens firm_bn_men_temporary firm_bn_women_pens firm_bn_women_temporary,  ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none)  nodepvar star(* 0.1 ** 0.05 *** 0.01)    ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			mgroups("\textbf{Preference for men}" "\textbf{Preference for women}",pattern(1 0 1 0)  span prefix(\multicolumn{@span}{c}{) suffix(})) ///
			mtitle( "Full sample" "Temporary jobs" "Full sample" "Temporary jobs") ///
			stats(controls noga bn firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation" "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")	
	esttab  firm_bn_men_pens firm_bn_men_temporary firm_bn_women_pens firm_bn_women_temporary using "$results_part_time\table_d10.tex", replace   ///
			varwidth(30) cells("b(star fmt(%9.2gc))" "se(par fmt(%9.2gc))")  label collabels(none)  nodepvar star(* 0.1 ** 0.05 *** 0.01)  frag  ///
			keep(1.pensum_cat 2.pensum_cat  )	///
			coeflabels(1.pensum_cat "Full- or part-time job" 2.pensum_cat "Part-time job" ) ///
			refcat(1.pensum_cat "Full-time job")  ///
			mgroups("\textbf{Preference for men}" "\textbf{Preference for women}",pattern(1 0 1 0)  span prefix(\multicolumn{@span}{c}{) suffix(})) ///
			mtitle( "Full sample" "Temporary jobs" "Full sample" "Temporary jobs") ///
			stats(controls noga bn firm mean_disp N, fmt(%9.0fc) label("Job ad characteristics" "Industry" "Occupation"  "Firm fixed effects" "Mean dependent variable" "Observations" )) ///		
			addnotes("Standard errors in parentheses" "Standard Errors clustered at firm level")	

		

*************
* Is there a relationship between the vote share for the parental leave referendum in a canton ...
* 	... and the probability that men are preferred for part-time jobs?
*************

* Merge results on parental leave referendum to the dataset
rename cod_kanton kanton_alternative_num
merge m:1  kanton_alternative_num using "Misc_files\abstimmung_vaterschaft.dta"
drop if _merge==2
rename kanton_alternative_num cod_kanton

gen yes_share = 1-no_share

preserve
	keep if parttime==1
	keep if preferred_gender!=999
	drop if auftraggeber==1
		
	bys cod_kanton: egen diff_bur = nvals(burnr_ag)
	
	collapse men women diff_bur (count) obs_men=men, by(cod_kanton)
	
	drop if obs_men<5
	drop if diff_bur<2
		
	tabstat men obs_men, by(cod_kanton)
		
	rename cod_kanton kanton_alternative_num
	merge 1:1  kanton_alternative_num using "Misc_files\abstimmung_vaterschaft.dta"
	drop if _merge==2
	
	gen yes_share = 1-no_share

	tabstat yes_share, by(kanton_alternative_lab)
	tabstat men, by(kanton_alternative_lab)
	tabstat obs_men, by(kanton_alternative_lab)

	cap drop 	xb_line_yes_share 	se_line_yes_share ci_upper_yes_share ci_lower_yes_share

	reg men yes_share 
		local eq        "Male preference = `: di  %5.1f _b[_cons]'"
		local eq        `"`eq' `=cond(_b[yes_share]>0, "+", "-")'"'
		local eq        `"`eq' `: di %4.2f abs(_b[yes_share])' "'
		local eq        `"`eq'(`: di %3.2f _se[yes_share]') * yes vote share"'
		local eq        `"`eq' + {&epsilon}"'   
	disp "`eq'"
	cap predict double xb_line_yes_share	, xb 
	
	cap predict double se_line_yes_share , stdp
	gen ci_upper_yes_share = xb_line_yes_share+se_line_yes_share*1.96
	gen ci_lower_yes_share = xb_line_yes_share-se_line_yes_share*1.96	
	
	local mlcolor "mlcolor(black)"		
	local msize "large"
	twoway  (scatter men 			yes_share   	 ,  mcolor(black) `mlcolor' mlwidth(thin) msize(`msize')) ///
			(line 	ci_upper_yes_share 	yes_share  	,  msymbol(none) lwidth(medthin) lcolor(black%60) lpattern(dash)  sort ) ///
			(line 	ci_lower_yes_share	yes_share  	,  msymbol(none) lwidth(medthin) lcolor(black%60) lpattern(dash)  sort ) ///
			(line 	xb_line_yes_share 	yes_share  	,  msymbol(none) lwidth(medthin) lcolor(black%60   ) ) ,    ///			
			graphregion(color(white))  legend(off) ///
			xtitle(Share of yes votes in parental leave referendum, size(medlarge)) ///
			ytitle(Part-time job ads preferring men, size(medlarge)) ///
			xlabel(,labsize(medlarge)) ylabel(,labsize(medlarge))
	graph export "$results_part_time\figure_d11_b.eps" , as(eps) replace	
	* Produce table to have numerical estimates		
	export delimited using "$results_part_time/figure_d11_b_table.csv", replace	
restore

